//
//  ZoneAlertTableViewCell.m
//  EZ Tracker
//
//  Created by FENGYL on 2017/3/30.
//  Copyright © 2017年 FENGYL. All rights reserved.
//

#import "ZoneAlertTableViewCell.h"
#import "NetworkCommand.h"
#import "ZoneAlertsViewController.h"

#import <SVProgressHUD.h>

@interface ZoneAlertTableViewCell()

@property (strong, nonatomic) NSDictionary *responseObjectDictionary;

@property (weak, nonatomic) IBOutlet UILabel *fenceNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *zoneTypeLabel;
@property (weak, nonatomic) IBOutlet UILabel *distanceLabel;
@property (weak, nonatomic) IBOutlet UIButton *deleteButton;

@end

@implementation ZoneAlertTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    
}

-(void)setUpFenceName:(NSString *)fenceName zoneType:(NSString *)zoneType distance:(NSString *)distance responseObjectDictionary:(NSDictionary *)responseObjectDictionary{
    
    [self.fenceNameLabel setText:fenceName];
    [self.zoneTypeLabel setText:zoneType];
    [self.distanceLabel setText:distance];
    self.responseObjectDictionary = responseObjectDictionary;
    
}

-(void)deleteButtonHidden{
    
    self.deleteButton.hidden = !self.deleteButton.hidden;
    
}

- (IBAction)touchDeletebutton:(id)sender {
    
    NSDictionary *dict = self.responseObjectDictionary;
    
    NSString *pointsString = [dict valueForKey:NC_POINTS];
    NSString *fenceName = [dict valueForKey:NC_FENCE_NAME];
    NSNumber *fenceId = [dict valueForKey:NC_BIND_FENCE_ID];
    
    [SVProgressHUD showWithStatus:@"Loading..."];
    
    [NetworkCommand DelGeoFenceForPoints:pointsString fenceName:fenceName fenceId:fenceId Success:^(id responseObject) {
        
        NSDictionary *dict = responseObject;
        
        int status = [[dict valueForKey:NC_STATUS] intValue];
        
        if (status != 1) {
            
            [SVProgressHUD showErrorWithStatus:@"Failed"];
            [SVProgressHUD dismissWithDelay:1];
            
            return;
            
        }
        
        [SVProgressHUD dismiss];
        
        ZoneAlertsViewController *zoneAlertsViewController;
        
        for (UIView* next = [self superview]; next; next = next.superview) {
            UIResponder *nextResponder = [next nextResponder];
            if ([nextResponder isKindOfClass:[ZoneAlertsViewController class]]) {
                
                
                 zoneAlertsViewController = (ZoneAlertsViewController *)nextResponder;
                
            }
        }
        
        [zoneAlertsViewController reloadFenceTableView];
        
    } failure:^{
        
        [SVProgressHUD showErrorWithStatus:@"Failed"];
        [SVProgressHUD dismissWithDelay:1];
        
    }];
    
}


@end
